#include <sys/time.h>
#include <shadesmar/pubsub/subscriber.h>
#include <signal.h>

typedef struct img_data 
{
    int index;
    int size;
    // long int time;
    unsigned char  data[1920*1080*3];
};

void callback(shm::memory::Memblock *msg) 
{
  // `msg->ptr` to access `data`
  // `msg->size` to access `data_size`

  // The memory will be free'd at the end of this callback.
  // Copy to another memory location if you want to persist the data.
  // Alternatively, if you want to avoid the copy, you can call
  // `msg->no_delete()` which prevents the memory from being deleted
  // at the end of the callback.
  img_data *img;
  img = (img_data *)msg->ptr;
    struct timeval begin;
    gettimeofday(&begin,NULL);
    long long beginTime = (long long)begin.tv_sec * 1000 + (long long)begin.tv_usec / 1000;
    // printf("%ld %ld \n", beginTime, img->time);
  printf("[ms] img->index:%d \n", img->index);

}
shm::pubsub::Subscriber *sub;
void Stop(int signo) 
{
  // shm::pubsub::LockType::PthreadReadWriteLock::unlock;
  delete sub;
    printf("oops! stop!!!\n");
    _exit(0);
}

int main() 
{
    signal(SIGINT, Stop);
    // shm::pubsub::Subscriber sub("topic_name", callback);
    sub = new shm::pubsub::Subscriber("topic_name", callback);

    // Using `spin_once` with a manual loop
    while(true) 
    {
        sub->spin_once();
        usleep(100);
    }
    // OR
    // Using `spin`
    // sub.spin();
    
}